package com.hzqy.service.statistics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.hzqy.commons.utils.Base64;
import com.hzqy.persistence.dao.statistics.ActivityDao;
import com.hzqy.persistence.dao.statistics.TravelDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.AwardUserVo;
import com.hzqy.web.vo.PmsUserVo;
import com.hzqy.web.vo.travel.TravelUserVo;

@Service
public class TravelServiceBean extends GenericServiceBean<PmsUserVo, TravelDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private TravelDao travelDao;

	@Override
	public TravelDao getEntityDao() {
		return travelDao;
	}
	
	public Map<String,Object> selectTravelList(TravelUserVo travelUserVo, int curPage,
			int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = travelDao.selectTravelUserCount(travelUserVo);
		map.put("total", rowCount);
		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);
			map.put("f_travel_serverid", travelUserVo.getF_travel_serverid());
			map.put("f_travel_type", travelUserVo.getF_travel_type());
			map.put("f_travel_phone", travelUserVo.getF_travel_phone());
			map.put("f_travel_cid", travelUserVo.getF_travel_cid());
			map.put("beginDate", travelUserVo.getBeginDate());
			map.put("endDate", travelUserVo.getEndDate());
			List<TravelUserVo> results = travelDao.selectTravelUserList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<AwardUserVo>());
		}
		return map;
	}
	
	public Map<String,Object> selectExcelList(TravelUserVo travelUserVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		Integer rowCount = travelDao.selectExcelCount(travelUserVo);
		map.put("total", rowCount);
		if(rowCount > 0) {
			map.put("f_travel_serverid", travelUserVo.getF_travel_serverid());
			map.put("f_travel_type", travelUserVo.getF_travel_type());
			map.put("f_travel_phone", travelUserVo.getF_travel_phone());
			map.put("f_travel_cid", travelUserVo.getF_travel_cid());
			map.put("beginDate", travelUserVo.getBeginDate());
			map.put("endDate", travelUserVo.getEndDate());
			List<TravelUserVo> results = travelDao.selectExcelList(map);
			for (TravelUserVo auv : results) {
		        if ((auv.getF_travel_phone() != null) && (!"".equals(auv.getF_travel_phone())) && (!isPhone(auv.getF_travel_phone()))) {
		          byte[] tokenkey = Base64.decode(auv.getF_travel_phone());
		          String phone = new String(tokenkey);
		          auv.setF_travel_phone(phone);
		        }
		      }
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<TravelUserVo>());
		}
		return map;
	}
	 public static boolean isPhone(String phone)
	  {
	    String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
	    Pattern p = Pattern.compile(regex);
	    Matcher m = p.matcher(phone);
	    boolean isMatch = m.matches();
	    return isMatch;
	  }
}
